package com.ming.controller;

import com.ming.dao.CustomerMapper;
import com.ming.dao.RecipientMapper;
import com.ming.dao.StaffMapper;
import com.ming.dao.UserMapper;
import com.ming.pojo.Customer;
import com.ming.pojo.Recipient;
import com.ming.pojo.Staff;
import com.ming.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Xu
 * Created by Intellij IDEA.
 * @create 2021/4/18 11:42
 */
@Controller
public class UserController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    StaffMapper staffMapper;
    @Autowired
    RecipientMapper recipientMapper;
    @Autowired
    CustomerMapper customerMapper;

    @RequestMapping("/users")
    public String list(Model model){
        List<User> users = userMapper.queryUsers();
        model.addAttribute("users",users);
        return "users/list";
    }

    @RequestMapping("/staffUsers/{username}")
    public String staffUList(@PathVariable("username") String username,Model model){
        User user = userMapper.queryUserByUsername(username);
        System.out.println(user);
        model.addAttribute("user",user);
        return "users/staffUList";
    }
    @RequestMapping("/userUsers/{username}")
    public String userUList(@PathVariable("username") String username,Model model){
        User user = userMapper.queryUserByUsername(username);
        System.out.println(user);
        model.addAttribute("user",user);
        return "users/userUList";
    }
    @PostMapping("/updateStaffUser")
    public String updateSUser(User user){
        userMapper.updateUser(user);
        return "redirect:/index.html";
    }
    @GetMapping("/uUser/{username}")
    public String toUpdateUUser(@PathVariable("username") String username, Model model) {
        //查出原来的数据
        User user = userMapper.queryUserByUsername(username);
        Integer rid = recipientMapper.queryRecipientByName(username);
        Integer cid = customerMapper.queryCustomerByName(username);
        if(rid != null){
            Recipient recipient = recipientMapper.queryRecipientById(rid);
            model.addAttribute("someone",recipient);
        }
        if(cid != null){
            Customer customer = customerMapper.queryCustomerById(cid);
            model.addAttribute("someone",customer);
        }
        model.addAttribute("user", user);

        return "users/userUList";
    }
    @PostMapping("/updateUserUser")
    public String updateUUser(User user, Recipient recipient,Customer customer){
        userMapper.updateUser(user);
        //把更新结果填到客户表和收货人表
        Integer rid = recipientMapper.queryRecipientByName(user.getUsername());
        Integer cid = customerMapper.queryCustomerByName(user.getUsername());
        if(rid != null){
            recipientMapper.updateRecipient(new Recipient(rid,user.getUsername(),recipient.getSex(),recipient.getPhone(),recipient.getAddress()));
        }
        if(cid != null){
            customerMapper.updateCustomer(new Customer(cid,user.getUsername(),customer.getSex(),customer.getPhone(),customer.getAddress()));
        }
        return "redirect:/index.html";
    }

    @GetMapping("/user")
    public String toAddPage(Model model){
        //查出所有部门信息
        List<User> users = userMapper.queryUsers();
        model.addAttribute("users" ,users);
        return "users/add";
    }
    @PostMapping("/user")
    public String addUser(User user){
        userMapper.addUser(user);
        if(user.getAuthority() == 1 && staffMapper.queryStaffName(user.getUsername()) == null){
            Integer mId = staffMapper.getMId();
            staffMapper.addStaff(new Staff(mId,user.getUsername(),null,null,0));
        }
        return "redirect:/users";
    }


    //去用户的修改页面
    @GetMapping("/user/{username}")
    public String toUpdateUser(@PathVariable("username") String username, Model model) {
        //查出原来的数据
        User user = userMapper.queryUserByUsername(username);
        model.addAttribute("user", user);
        return "users/update";
    }

    @PostMapping("/updateUser")
    public String updateUser(User user) {
        userMapper.updateUser(user);
        if (user.getAuthority() == 1){
            Integer mId = staffMapper.getMId();
            staffMapper.addStaff(new Staff(mId,user.getUsername(),null,null,0));
        }
        return "redirect:/users";
    }


    @GetMapping("/deleteUser/{username}")
    public String deleteStaff(@PathVariable("username")String username){
        userMapper.deleteUser(username);
        Integer sid = staffMapper.queryStaffName(username);
        Integer cid = customerMapper.queryCustomerName(username);
        Integer rid = recipientMapper.queryRecipientName(username);
        if(sid != null){
            staffMapper.deleteStaff(sid);
        }
        if (cid != null){
            customerMapper.deleteCustomer(cid);
        }
        if (rid != null){
            recipientMapper.deleteRecipient(rid);
        }
        return "redirect:/users";
    }

}
